//
//  LXCWDetailViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/24.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXCWDetailViewController.h"
#import "LXTableViewCell.h"
#import "LXRemarkTabelViewCell.h"
#import "LXAnnexShowTableViewCell.h"
@interface LXCWDetailViewController()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSArray *data;
@property (nonatomic) NSMutableDictionary *loanDic;
@property (nonatomic) NSString *fundTypeStr;
@end

@implementation LXCWDetailViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    
    if (self.statisticsType == YINGFK ||
        self.statisticsType == YIFK) {
        self.fundTypeStr = [NSString getPaidNameWithFundType:self.model.fundType];
        [self layoutCustomNav:self.view title:[NSString stringWithFormat:@"%@记录",self.fundTypeStr]];
    }
    else{
        self.fundTypeStr = [NSString getCollectionNameWithFundType:self.model.fundType];
        [self layoutCustomNav:self.view title:[NSString stringWithFormat:@"%@记录",self.fundTypeStr]];
    }
    

    
    
    
    [self.tableView registerClass:[LXTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXRemarkTabelViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
    [self.tableView registerClass:[LXAnnexShowTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXAnnexShowTableViewCell class])];


    self.view.backgroundColor = White_Color;
    [self.view addSubview:self.tableView];
    
    [self loadData];
    
}
- (void)loadData{

    
    
    NSString *url;
    if (self.statisticsType == YINGFK ||
        self.statisticsType == YIFK) {
        url = GET_TmoneyoutflowDetail;
    }
    else{
        url = GET_TmoneyintoflowDetail;
    }
    
    
    [MSNetworkManager getRevertUrl:url params:@{@"id":self.model.ID} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess) {
        
            CheLiangModel *model = [CheLiangModel mj_objectWithKeyValues:resonseObj];
            
            self.model.pics = model.pics;
            [self.tableView reloadData];
            
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    
    
    
    
    
    
    
    
    
    self.loanDic  = [NSMutableDictionary dictionary];
    
    
    // 车牌号
    [self.loanDic setValue:self.model.carNum forKey:@"carNum"];
    [self.loanDic setValue:[NSString stringWithFormat:@"%@ %@",self.model.carBrand,self.model.carMode] forKey:@"carBrand"];
 
    if (self.statisticsType == YISK) {
        
        [self.loanDic setValue:@"已结清" forKey:@"settleState"];
        
        // 客户姓名
        if (!StringIsEmpty(self.model.payerName)) {
            [self.loanDic setValue:self.model.payerName forKey:@"customerName"];
        }
        else {
            [self.loanDic setValue:self.model.customerName forKey:@"customerName"];
        }
        // 应收日期
        [self.loanDic setValue:self.model.exceptReceiveDate forKey:@"date"];
        // 应收金额
        [self.loanDic setValue:[NSString toTen:self.model.amount] forKey:@"amount"];
        // 实收日期
        [self.loanDic setValue:self.model.receiveDate forKey:@"actualDate"];
        // 实收金额
        [self.loanDic setValue:[NSString toTen:self.model.receiveAmount] forKey:@"actualAmount"];
        // 付款人
        [self.loanDic setValue:self.model.receiveEmployeeName forKey:@"manageName"];
        
        // 应收类型
        NSString *fundType = [NSString getCollectionNameWithFundType:self.model.fundType];
        [self.loanDic setValue:fundType forKey:@"fundType"];
    }
    else {
        
        // 客户姓名
        if (!StringIsEmpty(self.model.payeeName)) {
            [self.loanDic setValue:self.model.payeeName forKey:@"customerName"];
        }
        else {
            [self.loanDic setValue:self.model.customerName forKey:@"customerName"];
        }
        [self.loanDic setValue:@"已付清" forKey:@"settleState"];
        
        // 应付日期
        [self.loanDic setValue:self.model.exceptPaidDate forKey:@"date"];
        // 应付金额
        [self.loanDic setValue:[NSString toTen:self.model.amount] forKey:@"amount"];
        // 实付日期
        [self.loanDic setValue:self.model.paidDate forKey:@"actualDate"];
        // 实付金额
        [self.loanDic setValue:[NSString toTen:self.model.paidAmount] forKey:@"actualAmount"];
        // 付款人
        [self.loanDic setValue:self.model.payEmployeeName forKey:@"manageName"];
        // 应付类型
        NSString *fundType = [NSString getPaidNameWithFundType:self.model.fundType];
        [self.loanDic setValue:fundType forKey:@"fundType"];
        
    }
    
    
    [self.loanDic setValue:self.model.remark forKey:@"remark"];


    self.data = @[
         
           @{
               @"title":@"车牌号",
               @"key":@"carNum",
               @"height":@(kComHeight)
           },
           @{
               @"title":@"品牌型号",
               @"key":@"carBrand",
               @"height":@(kComHeight)
           },
           @{
               @"title":self.statisticsType==YISK?(self.model.payerType==0?@"客户姓名":@"合作伙伴"):((self.model.payeeType==0?@"客户姓名":@"合作伙伴")),
               @"key":@"customerName",
               @"height":@(kComHeight),
           },
           @{
               @"title":self.statisticsType==YIFK||self.statisticsType==YINGFK?@"付款类型":@"收款类型",
           @"key":@"fundType",
           @"height":@(kComHeight),
          },
           @{
               @"title":self.statisticsType==YIFK||self.statisticsType==YINGFK?@"应付日期":@"应收日期",
               @"key":@"date",
               @"height":@(kComHeight),
           },
           @{
               @"title":self.statisticsType==YIFK||self.statisticsType==YINGFK?@"应付金额(元)":@"应收金额(元)",
               @"key":@"amount",
               @"height":@(kComHeight),
           },
           @{
               @"title":self.statisticsType==YIFK||self.statisticsType==YINGFK?@"实付日期":@"实收日期",
               @"key":@"actualDate",
               @"placeHolder":@"--",
               @"height":@(kComHeight),
           },
           @{
               @"title":self.statisticsType==YIFK||self.statisticsType==YINGFK?@"实付金额(元)":@"实收金额(元)",
               @"key":@"actualAmount",
               @"placeHolder":@"--",
               @"height":@(kComHeight),
           },
           @{
               @"title":@"状态",
               @"key":@"settleState",
               @"placeHolder" : @"--",
               @"height":@(kComHeight),
           },
           @{
               @"title":self.statisticsType==YIFK||self.statisticsType==YINGFK?@"付款人":@"收款人",
               @"key":@"manageName",
               @"placeHolder" : @"--",
               @"height":@(kComHeight),
           },
           @{
               @"title":[NSString stringWithFormat:@"%@照片",self.fundTypeStr],
               @"key":@"annex",
               @"height":@(kComHeight)
           },
           @{
               @"title":@"备注",
               @"key":@"remark",
               @"height":@([NSString remarksHeight:self.model.remark])
           }
    ];
    
  
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.data[indexPath.row];
    if ([dic[@"key"] isEqualToString:@"annex"]) {
        return [LXAnnexShowTableViewCell getCellHeightWithDataSource:self.model.pics];
    }
    return [dic[@"height"] doubleValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dic = self.data[indexPath.row];
    
    if([dic[@"key"] isEqualToString:@"annex"]){
         LXAnnexShowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXAnnexShowTableViewCell class])];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
         [cell setClipsToBounds:YES];
         cell.titleLab.text = dic[@"title"];
    
         NSArray *ary = self.model.pics;
         [cell reloadData:ary];
         // 放大图片
         [cell setCallBackExtendAnnex:^(NSInteger num,id obj) {
             LXBigImageView *view = [[LXBigImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) WithShowImageAry:ary];
             [view.scrollView setContentOffset:CGPointMake(ScreenWidth*num, 0) animated:NO];
             [[UIApplication sharedApplication].delegate.window addSubview:view];
             
         }];
        return cell;
     }
   else if([dic[@"key"]  isEqualToString:@"remark"]){
        // 备注
        LXRemarkTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
       cell.textView.userInteractionEnabled = NO;
        cell.remarkTitle.text = @"备注";
       cell.textView.wordNumLabel.hidden = YES;
        cell.textView.text = self.loanDic[@"remark"];
        return cell;
    }

    
    
    LXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setClipsToBounds:YES];
    

    cell.cellDic = dic;
    cell.textFiled.hidden = NO;
    cell.subTitleLab.hidden = YES;
    cell.textFiled.text = self.loanDic[dic[@"key"]];
    
    return cell;
}




- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, kScreenHeight - kViewTopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = ColorWithF7F8FA;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

@end
